<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 添加/编辑抬头 开发者: 麦沃德科技-半夏  
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 表单内容 -->
		<view class="container-main">
			<view class="main-item">
				<view class="item-title"><text class="required">*</text>发票类型</view>
				<view class="item-radio flex">
					<view class="option" :class="{active: formData.invoice_type == 1}" @click="formData.invoice_type = 1">普通发票</view>
					<view class="option" :class="{active: formData.invoice_type == 2}" @click="formData.invoice_type = 2">专用发票</view>
				</view>
			</view>
			<view class="main-item" v-if="formData.invoice_type == 1">
				<view class="item-title"><text class="required">*</text>抬头类型</view>
				<view class="item-radio flex">
					<view class="option" :class="{active: formData.title_type == 1}" @click="formData.title_type = 1">个人</view>
					<view class="option" :class="{active: formData.title_type == 2}" @click="formData.title_type = 2">单位</view>
				</view>
			</view>
			<view class="main-item">
				<view class="item-title"><text class="required">*</text>发票抬头</view>
				<view class="item-input">
					<input class="input" type="text" v-model="formData.invoice_title" placeholder="请输入发票抬头" placeholder-class="placeholder" />
				</view>
			</view>
			<block v-if="(formData.invoice_type == 1 && formData.title_type == 2) || formData.invoice_type == 2">
				<view class="main-item">
					<view class="item-title"><text class="required">*</text>单位税号</view>
					<view class="item-input">
						<input class="input" type="text" v-model="formData.unit_tax_number" placeholder="请输入单位税号" placeholder-class="placeholder" />
					</view>
				</view>
				<view class="main-item">
					<view class="item-title">注册地址</view>
					<view class="item-input">
						<input class="input" type="text" v-model="formData.registered_address" placeholder="请输入单位注册地址" placeholder-class="placeholder" />
					</view>
				</view>
				<view class="main-item">
					<view class="item-title">开户银行</view>
					<view class="item-input">
						<input class="input" type="text" v-model="formData.bank_of_deposit" placeholder="请输入开户银行" placeholder-class="placeholder" />
					</view>
				</view>
				<view class="main-item">
					<view class="item-title">银行行号</view>
					<view class="item-input">
						<input class="input" type="text" v-model="formData.bank_no" placeholder="请输入银行行号" placeholder-class="placeholder" />
					</view>
				</view>
			</block>
			<view class="main-item">
				<view class="item-title">联系电话</view>
				<view class="item-input">
					<input class="input" type="number" maxlength="11" v-model="formData.mobile" placeholder="请输入联系电话" placeholder-class="placeholder" />
				</view>
			</view>
			<view class="main-item">
				<view class="item-title">电子邮箱</view>
				<view class="item-input">
					<input class="input" type="text" v-model="formData.email" placeholder="请输入电子邮箱" placeholder-class="placeholder" />
				</view>
			</view>
			<view class="main-item">
				<view class="item-title">设置</view>
				<view class="item-switch flex flex-between-center">
					<view class="switch-title">是否默认抬头</view>
					<view class="switch-box" :class="{select: formData.is_default == 1}" @click="formData.is_default = formData.is_default == 1 ? 2 : 1">
						<view class="round"></view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="container-footer">
			<view class="footer-btn" @click="handleAdd()">{{formData.id ? "编辑抬头" : "添加抬头"}}</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 表单内容
				formData: {
					invoice_type: 1,
					title_type: 1,
					is_default: 2,
					invoice_title: "",
					mobile: "",
					email: "",
					unit_tax_number: "",
					registered_address: "",
					bank_of_deposit: "",
					bank_no: "",
				},
				// 延时器
				delayer: null,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad(option) {
			if (option.id) {
				uni.showLoading({
					title: "加载中"
				})
				this.getTitle(option.id, () => {
					uni.hideLoading()
					this.loadEnd = true
				})
			} else {
				this.loadEnd = true
			}
			this.$nextTick(() => {
				uni.setNavigationBarTitle({
					title: option.id ? "编辑抬头" : "添加抬头"
				})
			})
		},
		onUnload() {
			if (this.delayer) clearTimeout(this.delayer)
		},
		methods: {
			// 获取抬头内容
			getTitle(id, fn) {
				this.$util.request("passenger.invoice.title.details", {
					id,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.formData = res.data || {}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取抬头详情 ', error)
				})
			},
			// 添加/编辑抬头
			handleAdd() {
				if (!this.formData.invoice_title) {
					uni.showToast({
						icon: "none",
						title: "发票抬头不能为空"
					})
					return
				}
				if ((this.formData.invoice_type == 1 && this.formData.title_type == 2) || this.formData.invoice_type == 2) {
					if (!this.formData.unit_tax_number) {
						uni.showToast({
							icon: "none",
							title: "单位税号不能为空"
						})
						return
					}
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				let url = "passenger.invoice.title.add"
				let data = {
					invoice_type: this.formData.invoice_type,
					invoice_title: this.formData.invoice_title,
					mobile: this.formData.mobile || "",
					email: this.formData.email || "",
					is_default: this.formData.is_default,
				}
				if (this.formData.invoice_type == 1) {
					data.title_type = this.formData.title_type
				} else if (this.formData.invoice_type == 2) {
					data.title_type = 2
				}
				if (this.formData.invoice_type == 2 || this.formData.title_type == 2) {
					data.unit_tax_number = this.formData.unit_tax_number
					data.registered_address = this.formData.registered_address || ""
					data.bank_of_deposit = this.formData.bank_of_deposit || ""
					data.bank_no = this.formData.bank_no || ""
				}
				if (this.formData.id) {
					url = "passenger.invoice.title.edit"
					data.id = this.formData.id
				}
				this.$util.request(url, data).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						uni.showToast({
							icon: "success",
							title: this.formData.id ? "编辑成功" : "添加成功",
							duration: 1500,
							mask: true
						})
						clearTimeout(this.delayer)
						this.delayer = setTimeout(() => {
							if (getCurrentPages().length == 1) {
								this.$util.toPage({
									mode: 2,
									path: '/pages/passenger/mine/invoice/title',
									isToken: true,
								})
							} else {
								uni.navigateBack()
							}
						}, 1500);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('添加抬头 ', error)
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #ffffff;
	}

	.container {
		padding-bottom: 112rpx;

		.container-main {
			padding: 32rpx;

			.main-item {
				margin-top: 32rpx;

				&:first-child {
					margin-top: 0;
				}

				.item-title {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 44rpx;

					.required {
						color: #E60012;
					}
				}

				.item-radio {
					margin-top: 24rpx;

					.option {
						color: #ACADB7;
						font-size: 28rpx;
						line-height: 40rpx;
						padding: 36rpx 32rpx;
						border-radius: 16rpx;
						background: #F6F7FB;
						margin-left: 32rpx;

						&:first-child {
							margin-left: 0;
						}

						&.active {
							background: var(--theme-color);
							color: #ffffff;
						}
					}
				}

				.item-input {
					margin-top: 24rpx;
					border-radius: 16rpx;
					background: #F6F7FB;

					.input {
						padding: 36rpx 32rpx;
						color: #242629;
						font-size: 28rpx;
						line-height: 40rpx;
					}

					.placeholder {
						color: #ACADB7;
						font-size: 28rpx;
						line-height: 40rpx;
					}
				}

				.item-switch {
					margin-top: 24rpx;
					padding: 32rpx;
					border-radius: 16rpx;
					background: #F6F7FB;

					.switch-title {
						color: #242629;
						font-size: 28rpx;
						line-height: 40rpx;
					}

					.switch-box {
						width: 96rpx;
						height: 48rpx;
						padding: 4rpx;
						background: #DEDEDE;
						border-radius: 24rpx;
						transition: all .3s;

						.round {
							width: 40rpx;
							height: 40rpx;
							border-radius: 50%;
							background: #fff;
							margin-left: 0;
							transition: all .3s;
						}

						&.select {
							background: var(--theme-color);

							.round {
								margin-left: calc(100% - 40rpx);
							}
						}
					}
				}
			}
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 99;
			padding: 12rpx 32rpx;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;

			.footer-btn {
				color: #ffffff;
				font-size: 32rpx;
				line-height: 44rpx;
				padding: 22rpx 24rpx;
				border-radius: 40rpx;
				text-align: center;
				background: var(--theme-color);
			}
		}
	}
</style>